﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Drawing;
using System.Configuration;

namespace Check_Printing_Solution
{
    public class PrintLabelEngine
    {
        const int LABELS_PER_PAGE = 6;
        const string FONT = "Helvetica";
        const int FONT_SIZE = 9;
        int PADDING = Convert.ToInt32( ConfigurationManager.AppSettings["LabelNameX"].ToString() );

        PrintManager manager = new PrintManager();

        private int NameColumn { get { return 15; } }
        private int Street1Column { get { return 16; } }
        private int Street2Column { get { return 17; } }
        private int CityColumn { get { return 18; } }
        private int TelephoneColumn { get { return 19; } }
        private DataSet DS { get; set; }
        private DataRow CurrentRow { get; set; }
        private int RowPosition { get; set; }

        private void Print( string printername )
        {
            int i = RowPosition;

            while ( RowPosition < DS.Tables["Customers"].Rows.Count )
            {
                PrintDocument template = new PrintDocument();
                template.PrintPage += new PrintPageEventHandler( template_PrintPage );
                template.QueryPageSettings += new QueryPageSettingsEventHandler( template_QueryPageSettings );
                template.PrinterSettings.PrinterName = printername;

                PageSettings margins = new PageSettings();
                template.DefaultPageSettings.Margins.Left = 0;
                template.DefaultPageSettings.Margins.Top = 0;
                template.DefaultPageSettings.PaperSize = new PaperSize( "Label Paper", 400, 600 );

                template.Print();
            }
        }

        void template_QueryPageSettings( object sender, QueryPageSettingsEventArgs e )
        {
            Margins newMargins = new Margins( 0, 0, 0, 0 );
            e.PageSettings.Margins = newMargins;
        }

        void template_PrintPage( object sender, PrintPageEventArgs e )
        {
            int padding = 0;

            for ( int j = 0; j < LABELS_PER_PAGE; j++ )
            {
                if ( RowPosition < DS.Tables["Customers"].Rows.Count )
                {
                    e.Graphics.DrawString( DS.Tables["Customers"].Rows[j][NameColumn].ToString(),
                        new Font( FONT, Convert.ToSingle( FONT_SIZE ), FontStyle.Regular ),
                        Brushes.Black,
                        new Point(
                            Convert.ToInt32( ConfigurationManager.AppSettings["LabelNameX"].ToString() ),
                            Convert.ToInt32( ConfigurationManager.AppSettings["LabelNameY"].ToString() ) + padding ) );

                    e.Graphics.DrawString( DS.Tables["Customers"].Rows[j][Street1Column].ToString(),
                        new Font( FONT, Convert.ToSingle( FONT_SIZE ), FontStyle.Regular ),
                        Brushes.Black,
                        new Point(
                            Convert.ToInt32( ConfigurationManager.AppSettings["LabelStreet1X"].ToString() ),
                            Convert.ToInt32( ConfigurationManager.AppSettings["LabelStreet1Y"].ToString() ) + padding ) );

                    e.Graphics.DrawString( DS.Tables["Customers"].Rows[j][Street2Column].ToString(),
                        new Font( FONT, Convert.ToSingle( FONT_SIZE ), FontStyle.Regular ),
                        Brushes.Black,
                        new Point(
                            Convert.ToInt32( ConfigurationManager.AppSettings["LabelStreet2X"].ToString() ),
                            Convert.ToInt32( ConfigurationManager.AppSettings["LabelStreet2X"].ToString() ) + padding ) );

                    e.Graphics.DrawString( DS.Tables["Customers"].Rows[j][CityColumn].ToString(),
                        new Font( FONT, Convert.ToSingle( FONT_SIZE ), FontStyle.Regular ),
                        Brushes.Black,
                        new Point(
                            Convert.ToInt32( ConfigurationManager.AppSettings["LabelCityX"].ToString() ),
                            Convert.ToInt32( ConfigurationManager.AppSettings["LabelCityY"].ToString() ) + padding ) );

                    e.Graphics.DrawString( DS.Tables["Customers"].Rows[j][TelephoneColumn].ToString(),
                        new Font( FONT, Convert.ToSingle( FONT_SIZE ), FontStyle.Bold ),
                        Brushes.Black,
                        new Point(
                            Convert.ToInt32( ConfigurationManager.AppSettings["LabelTelePhoneX"].ToString() ),
                            Convert.ToInt32( ConfigurationManager.AppSettings["LabelTelePhoneY"].ToString() ) + padding ) );
                }

                RowPosition++;
                padding = padding + PADDING;
            }
        }

        public void InitiatePrint( DataSet ds, string printername )
        {
            DS = ds;
            Print( printername );
        }
    }
}
